# get the facebook id from facebook payloads

# FACEBOOK_PAYLOADS = "./facebook_payloads/"
FACEBOOK_PAYLOADS = "./http_payload/"
FACEBOOK_ID_LIST = "./facebook_ids/"

REG_FACEBOOK_ID = /c_user+\b*=\b*(\d+);/

NON_FACEBOOK_IP_FILE = "./non_facebook_ip.txt"

module GetFacebookID


  def self.get_all_files
    Common.get_all_files(FACEBOOK_PAYLOADS)    
  end

  def self.handle_all_files(all_files)    facebook_id_mapping = {}

    all_files.each do |file_name|

      mapping = handle_each_file file_name

      # facebook_id_array = facebook_id_array + return_array.uniq
      mapping.each do |key, value|
        if facebook_id_mapping.has_key? key
          facebook_id_mapping[key] = facebook_id_mapping[key] + "|" + value 
        else
          facebook_id_mapping[key] = value 
        end
      end

    end

    # facebook_id_array.uniq
    facebook_id_mapping
  end

  def self.handle_each_file(file_name)
    puts "now handling : " + file_name
    file = File.open FACEBOOK_PAYLOADS + file_name, "r"
    non_facebook_ip = File.open NON_FACEBOOK_IP_FILE, "a"
    facebook_id_mapping = {}

    begin
      counter = 0
      while line = file.readline
        if line.include? "facebook"
          fields = line.split("|")

          cookie = fields[15]
          client_ip = fields[5]# | "NULL" 
          client_port = fields[6]
          referer = fields[7].downcase
          host = fields[8].downcase

          if (referer.include? "facebook" or referer.include? "fbcdn" or host.include? "facebook" or host.include? "fbcdn") and cookie and REG_FACEBOOK_ID.match cookie.downcase
            key = $1
            value = client_ip #  + ":" + client_port
            # puts counter
            if (facebook_id_mapping.has_key? key) and !(facebook_id_mapping[key].include? value)
              facebook_id_mapping[key] = facebook_id_mapping[key] + "|" + value 
            elsif !(facebook_id_mapping.has_key? key)
              facebook_id_mapping[key] = value 
            else
              # the value exist, do nothing
            end
          else
            # if facebook_id_mapping.has_key? "non"
            #   facebook_id_mapping["non"] = client_ip
            # else              
            #   facebook_id_mapping["non"] = facebook_id_mapping["non"] + "|" + client_ip
            # end
          end
        else
          # non_facebook_ip.puts client_ip
        end

        counter = counter + 1
      end
    rescue Exception => e
      puts e.to_s
    end

    # output each file the facebook_list
    # output_file = File.open FACEBOOK_ID_LIST + file_name, "w"

    # fb_id_list = facebook_id_array.uniq
    # fb_id_list.each { |id| output_file.puts id }
    # facebook_id_mapping.each do |key, value|
    #   output_file.puts "#{key}@#{value}"
    # end

    # output_file.close
    # non_facebook_ip.close

    facebook_id_mapping
  end


  def self.run
    puts ">> start to get facebook IDs ..."

    begin
      all_files = GetFacebookID.get_all_files      
      all_mapping = GetFacebookID.handle_all_files all_files

      output = File.open "all_facebook_ids.txt", "w"
      all_mapping.each do |key, value| 
        value_field = (value.split "|").uniq
        output.puts "#{key}@#{value_field.join("|")}" 
      end
      output.close

      puts "----------->> end to get facebook IDs"
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end
end
